using System.Collections;
using UnityEngine;

public class AnimationHelper
{
	public static IEnumerator MoveGameObjectToGameObject(GameObject objectToMove, GameObject destiny, float time, AnimationCurve curve = null)
	{
		float currentTime = 0f;
		Vector3 origin = objectToMove.transform.position;
		Vector3 difference = destiny.transform.position - objectToMove.transform.position;
		while (currentTime < time)
		{
			float completenessRatio = currentTime / time;
			float progress = ApplyEasing(completenessRatio, curve);
			Vector3 position = origin + difference * progress;
			objectToMove.transform.position = position;
			currentTime += Time.deltaTime;
			yield return null;
		}
		objectToMove.transform.position = destiny.transform.position;
	}

	public static IEnumerator MoveGameObjectToPosition(GameObject objectToMove, Vector3 destiny, float time, AnimationCurve curve = null)
	{
		float currentTime = 0f;
		Vector3 origin = objectToMove.transform.position;
		Vector3 difference = destiny - objectToMove.transform.position;
		while (currentTime < time)
		{
			float completenessRatio = currentTime / time;
			float progress = ApplyEasing(completenessRatio, curve);
			Vector3 position = origin + difference * progress;
			objectToMove.transform.position = position;
			currentTime += Time.deltaTime;
			yield return null;
		}
		objectToMove.transform.position = destiny;
	}

	public static IEnumerator ScaleGameObject(GameObject objectToScale, float scaleFactor, float time, AnimationCurve curve = null)
	{
		float currentTime = 0f;
		Vector3 origin = objectToScale.transform.localScale;
		Vector3 difference = objectToScale.transform.localScale * scaleFactor - objectToScale.transform.localScale;
		while (currentTime < time)
		{
			float completenessRatio = currentTime / time;
			float progress = ApplyEasing(completenessRatio, curve);
			Vector3 scale = origin + difference * progress;
			objectToScale.transform.localScale = scale;
			currentTime += Time.deltaTime;
			yield return null;
		}
		objectToScale.transform.localScale = origin * scaleFactor;
	}

	public static IEnumerator ScaleGameObject(GameObject objectToScale, Vector3 finalScale, float time, AnimationCurve curve = null)
	{
		float currentTime = 0f;
		Vector3 origin = objectToScale.transform.localScale;
		Vector3 difference = finalScale - objectToScale.transform.localScale;
		while (currentTime < time)
		{
			float completenessRatio = currentTime / time;
			float progress = ApplyEasing(completenessRatio, curve);
			Vector3 scale = origin + difference * progress;
			objectToScale.transform.localScale = scale;
			currentTime += Time.deltaTime;
			yield return null;
		}
		objectToScale.transform.localScale = finalScale;
	}

	public static IEnumerator RotateGameObject(GameObject objectToRotate, float finalRotation, float time, AnimationCurve curve = null)
	{
		float currentTime = 0f;
		Vector3 eulerAngles = objectToRotate.transform.rotation.eulerAngles;
		float origin = eulerAngles.z;
		while (currentTime < time)
		{
			float completenessRatio = Mathf.Min(currentTime / time, 1f);
			float progress = ApplyEasing(completenessRatio, curve);
			objectToRotate.transform.localRotation = Quaternion.Euler(0f, 0f, Mathf.LerpAngle(origin, finalRotation, progress));
			currentTime += Time.deltaTime;
			yield return null;
		}
		objectToRotate.transform.localRotation = Quaternion.Euler(0f, 0f, finalRotation);
	}

	private static float ApplyEasing(float completenessRatio, AnimationCurve curve)
	{
		return curve?.Evaluate(completenessRatio) ?? completenessRatio;
	}
}
